package global.com.loansapplication.comm;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import global.com.loansapplication.R;


/**
 *  Created by admin on 2017/8/4.
 *  app 更新下载
 */
public class DownloadService extends IntentService {
    String apk = Environment.getExternalStorageDirectory().getPath() + File.separator +"che7estore.apk";

    public DownloadService() {
        super("download");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        URL url;
        HttpURLConnection http;
        BufferedInputStream in;
        BufferedOutputStream out;
        try {
			url = new URL(arg0.getStringExtra("url"));
            http = (HttpURLConnection) url.openConnection();
            http.setRequestMethod("GET");
            http.setConnectTimeout(5000);
            http.setReadTimeout(5000);
            http.setDoInput(true);
            http.setInstanceFollowRedirects(true);
            http.setRequestProperty("Accept-Encoding", "identity");
            http.connect();
            File file = new File(apk);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            in = new BufferedInputStream(http.getInputStream());
            out = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buffer = new byte[1024 * 8];
            int len;
            Notification.Builder builder = new Notification.Builder(getBaseContext()).setSmallIcon(R.mipmap.ic_launcher_round)
                    .setTicker("正在下载")
                    .setContentTitle("正在下载");
            while ((len = in.read(buffer)) != -1) {
//                builder.setProgress(http.getContentLength(), (int) file.length(), false);
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, builder.build());
//                }
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            in.close();
            http.disconnect();
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setTicker("下载完成!")
                    .setContentTitle("点击安装")
                    .setContentText("开始安装")
                    .setContentIntent(pi)
                    .setWhen(System.currentTimeMillis());
//			startForeground(0, notification);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, builder.build());
            }else{
                startActivity(i);
            }
        } catch (IOException e) {
            Log.e("Download", e.getMessage());
        }
    }
}
